package v2;

import lombok.Data;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;

import java.io.IOException;
import java.util.Collections;

@Data
public class LinkDoc implements Doc {

    private String link;

    private String body;

    private String title;


    public static LinkDoc of(String link, String body, String title) {
        LinkDoc doc = new LinkDoc();
        doc.setLink(link);
        doc.setBody(body);
        doc.setTitle(title);
        return doc;
    }

    public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException {
        LinkDoc doc = LinkDoc.of(
                "http://hao123.com",
                "我喜欢你",
                "这个世界好美好"

        );

        IndexTemplateV2 templateV2 = new IndexTemplateV2();
        System.out.println(templateV2.index(Collections.<Doc>singleton(doc), LinkDoc.class));

        System.out.println(templateV2.find(LinkDoc.class, "世界"));


    }

    @Override
    public Document toDoc() {
        Document doc = new Document();
        doc.add(new TextField("body", body, Field.Store.YES));
        doc.add(new TextField("title", title, Field.Store.YES));
        doc.add(new StringField("link", link, Field.Store.YES));
        return doc;
    }

    @Override
    public Doc fromDoc(Document doc) {
        body = doc.get("body");
        link = doc.get("link");
        title = doc.get("title");
        return this;
    }

    @Override
    public String pk() {
        return link;
    }
}
